﻿Imports TransportadoraToledoBLL
Imports System.IO
Public Class frm_lotacao_veiculo
    Dim objA As LotacaoBLL
    Dim objC As VeiculosBLL
    Dim protocolo As String
    Dim valor As Double
    Dim objCargas As LotacaoBLL
    Public controle As String
    Public ultimo, primeiro, atual, codigo As Integer
    Dim tab, tab2 As DataTable
    Dim cont As Integer = 0

    'eeeee
#Region "Métodos"
    Public Sub carregaPropriedades()
        Try
            objA = New LotacaoBLL
            If (tbCodigo.Text = "") Then
                objA.pLot_codigo = 0
            Else
                objA.pLot_codigo = tbCodigo.Text
            End If
            If (tbCodVeiculo.Text = "") Then
                objA.pVei_codigo = 0
            Else
                objA.pVei_codigo = tbCodVeiculo.Text
            End If
            objA.pLot_data = dtCarregamento.Value.Date
            objA.pLot_partida = dtPrevPartida.Value.Date
            objA.pLot_status = cbStatus.SelectedItem
        Catch ex As Exception
            MessageBox.Show("Erro: " & ex.Message, "", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub
    Public Function contaCargas(ByVal tipo As Char, ByVal inicio As Char) As Integer

        Try
            cont = 0
            'If (inicio = "S") Then
            For Each linha As DataRow In tab2.Rows
                cont = cont + 1
            Next
            'Else
            '    If (tipo = "A") Then
            '        cont = cont + 1
            '    Else
            '        cont = cont - 1
            '    End If
            'End If
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
        Return cont
    End Function

    Public Function contaPeso() As Integer

        Try
            Dim peso As Decimal
            peso = 0
            Dim linha As DataRow
            For Each linha In tab2.Rows
                peso += Decimal.Parse(linha("car_peso").ToString())
            Next
            lpeso.Text = peso.ToString()
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
        Return cont
    End Function

    Public Sub localizarVeiculoLotacao()
        Try
            objA = New LotacaoBLL
            objA.localizar(tbCodigo.Text)

            objC = New VeiculosBLL
            objC.localizar(Integer.Parse(objA.pVei_codigo))
            tbCapacidadeVeiculo.Text = objC.pCapacidade
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        Finally
            objC = Nothing
        End Try
    End Sub



    Public Sub carregaCampos()
        Try
            tbCodigo.Text = objA.pLot_codigo
            dtCarregamento.Value = objA.pLot_data
            dtPrevPartida.Value = objA.pLot_partida
            cbStatus.SelectedItem = objA.pLot_status
            localizarVeiculoLotacao()
            associaGriCargas()
            objA = Nothing
        Catch ex As Exception
            MessageBox.Show("Erro", "", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub

    Public Sub associaGriCargas()
        Try
            tab2 = New DataTable()

            tab2.Rows.Clear()
            tab2.Columns.Clear()

            tab2.Columns.Add("car_codigo", GetType(Integer))
            tab2.Columns.Add("age_codigo", GetType(Integer))
            tab2.Columns.Add("car_peso", GetType(Decimal))
            tab2.Columns.Add("rot_distancia", GetType(Decimal))
            tab2.Columns.Add("age_data_previsao", GetType(Date))

            If tbCodigo.Text <> "" Then

                tab2 = objA.carregaListaCargas(tbCodigo.Text)

            End If

            dgCargas.DataSource = tab2
            dgCargas.Columns("car_codigo").HeaderText = "Carga"
            dgCargas.Columns("age_codigo").HeaderText = "Agendamento"
            dgCargas.Columns("car_peso").HeaderText = "Peso"
            dgCargas.Columns("rot_distancia").HeaderText = "Distância"
            dgCargas.Columns("age_data_previsao").HeaderText = "Previsão"
            dgCargas.Columns("car_codigo").Width = 70
            dgCargas.Columns("age_codigo").Width = 70
            dgCargas.Columns("car_peso").Width = 70
            dgCargas.Columns("rot_distancia").Width = 100
            dgCargas.Columns("age_data_previsao").Width = 100

        Catch ex As Exception
            MessageBox.Show("Erro", "", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub

    Public Sub carregaGriCargas()
        Try
            tab2 = New DataTable()

            objA = New LotacaoBLL()

            tab2.Rows.Clear()
            tab2.Columns.Clear()

            tab2.Columns.Add("car_codigo", GetType(Integer))
            tab2.Columns.Add("age_codigo", GetType(Integer))
            tab2.Columns.Add("car_peso", GetType(Decimal))
            tab2.Columns.Add("rot_distancia", GetType(Decimal))
            tab2.Columns.Add("age_data_previsao", GetType(Date))
            objA.pVei_codigo = tbCodVeiculo.Text
            tab2 = objA.carregarVeiculo()

            dgCargas.DataSource = tab2
            dgCargas.Columns("car_codigo").HeaderText = "Carga"
            dgCargas.Columns("age_codigo").HeaderText = "Agendamento"
            dgCargas.Columns("car_peso").HeaderText = "Peso"
            dgCargas.Columns("rot_distancia").HeaderText = "Distância"
            dgCargas.Columns("age_data_previsao").HeaderText = "Previsão"
            dgCargas.Columns("car_codigo").Width = 70
            dgCargas.Columns("age_codigo").Width = 70
            dgCargas.Columns("car_peso").Width = 70
            dgCargas.Columns("rot_distancia").Width = 100
            dgCargas.Columns("age_data_previsao").Width = 100

        Catch ex As Exception
            MessageBox.Show("Erro", "", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub

    Public Sub limpar()
        tbCodCarga.Text = ""
        tbCodigo.Text = ""
        tbCodVeiculo.Text = ""
        tbCapacidadeVeiculo.Text = ""
        dtCarregamento.Value = Date.Now
        dtPrevPartida.Value = Date.Now
        cbStatus.SelectedIndex = 0
        associaGriCargas()
        lqtdCargas.Text = 0
        lpeso.Text = 0
    End Sub

    Sub carregaPrimeiroUltimo()
        Try
            objA = New LotacaoBLL
            ultimo = objA.localizarPrimeiroUltimo("ultimo")
            primeiro = objA.localizarPrimeiroUltimo("primeiro")
            atual = ultimo
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
        objA = Nothing
    End Sub

    Sub carregaUltimo()
        Try
            If (ultimo <> 0) Then
                'algum usuario foi clicado
                objA = New LotacaoBLL
                objA.localizar(ultimo)
                tbCodigo.Text = objA.pLot_codigo
                tbCodVeiculo.Text = objA.pVei_codigo
                If (objA.pLot_codigo <> 0) Then
                    carregaCampos()
                End If
            End If

        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub
    Sub carregaPrimeiro()
        If (primeiro <> 0) Then
            'algum usuario foi clicado
            objA = New LotacaoBLL
            'tab2 = objA.localizar(primeiro)
            If (objA.pLot_codigo <> 0) Then
                carregaCampos()
            End If
        End If
        objA = Nothing
    End Sub
    Sub status(ByVal sta As Boolean)

        If (tbCodigo.Text = "" Or controle = "A") Then
            bIncluir.Enabled = sta
            If (tbCodigo.Text = "") Then
                bAlterar.Enabled = False
                bVaPara.Enabled = False
                bPesquisar.Enabled = False
                bExcluir.Enabled = False
                gbConsulta.Enabled = False
            Else
                bExcluir.Enabled = sta
                bPesquisar.Enabled = sta
                bAlterar.Enabled = sta
                bVaPara.Enabled = sta
            End If
            bCancelar.Enabled = Not sta
            bSalvar.Enabled = Not sta
            bPrimeiro.Enabled = sta
            bProximo.Enabled = sta
            bUltimo.Enabled = sta
            bAnterior.Enabled = sta
            gbConteudo.Enabled = Not sta

        Else
            bIncluir.Enabled = sta

            If (tbCodigo.Text = "") Then
                bExcluir.Enabled = False
                bAlterar.Enabled = False
                bVaPara.Enabled = False
                bPesquisar.Enabled = False
            Else
                bExcluir.Enabled = sta
                bAlterar.Enabled = sta
                bVaPara.Enabled = sta
                bPesquisar.Enabled = sta
            End If

            bCancelar.Enabled = Not sta
            bSalvar.Enabled = Not sta

            gbConteudo.Enabled = Not sta
        End If

        If (atual = primeiro And atual = ultimo) Then
            bProximo.Enabled = False
            bUltimo.Enabled = False
            bAnterior.Enabled = False
            bPrimeiro.Enabled = False
        ElseIf (atual = ultimo) Then
            bProximo.Enabled = False
            bUltimo.Enabled = False
            bAnterior.Enabled = True
            bPrimeiro.Enabled = True
        ElseIf (atual = primeiro) Then
            bProximo.Enabled = True
            bUltimo.Enabled = True
            bAnterior.Enabled = False
            bPrimeiro.Enabled = False
        Else
            bProximo.Enabled = True
            bUltimo.Enabled = True
            bAnterior.Enabled = True
            bPrimeiro.Enabled = True
        End If
    End Sub
#End Region

    Private Sub bIncluir_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bIncluir.Click
        limpar()
        controle = "I"
        status(False)
        cont = 0
    End Sub

    Private Sub bAlterar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bAlterar.Click
        bVeiculo.Enabled = False
        controle = "A"
        status(False)
    End Sub

    Private Sub bExcluir_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bExcluir.Click
        Try
            If (MessageBox.Show("Deseja excluir?", "", MessageBoxButtons.YesNo) = DialogResult.Yes) Then
                carregaPropriedades()
                objA.excluir()
                MessageBox.Show("Excluido com sucesso!")
                limpar()
                carregaPrimeiroUltimo()
                carregaUltimo()
                status(True)
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub

    Private Sub bSalvar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bSalvar.Click
        Try


            If tbCodVeiculo.Text <> "" Then
                If dgCargas.Rows.Count <> 0 Then
                    carregaPropriedades()
                    If (controle = "I") Then
                        objA.inserir()
                        For Each linha As DataRow In tab2.Rows
                            Dim objCargas As New LotacaoBLL
                            objCargas.inserirCargas(objA.pLot_codigo, linha("car_codigo"), linha("age_codigo"))
                            objCargas = Nothing
                        Next

                        MessageBox.Show("Incluido com sucesso!", "TransToledo", MessageBoxButtons.OK, MessageBoxIcon.Asterisk)
                        limpar()
                        carregaPrimeiroUltimo()
                        carregaUltimo()
                        status(True)
                    Else
                        objA.alterar()
                        For Each linha As DataRow In tab2.Rows
                            Dim objCargas As New LotacaoBLL
                            objCargas.alterarCargas(objA.pLot_codigo, linha("car_codigo"), linha("age_codigo"))
                            objCargas = Nothing
                        Next
                        MessageBox.Show("Alterado com sucesso!", "TransToledo", MessageBoxButtons.OK, MessageBoxIcon.Asterisk)
                        status(True)
                    End If
                    cont = 0
                    contaCargas("A", "S")
                    lqtdCargas.Text = cont
                    contaPeso()
                Else
                    MessageBox.Show("Não há cargas na lotação!", "TransToledo", MessageBoxButtons.OK, MessageBoxIcon.Asterisk)
                End If
            Else
                MessageBox.Show("Defina um veículo para ser carregado!", "TransToledo", MessageBoxButtons.OK, MessageBoxIcon.Asterisk)

            End If


        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub

    Private Sub bCancelar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bCancelar.Click
        If (controle = "I") Then
            carregaPrimeiroUltimo()
            carregaUltimo()
        End If
        contaCargas("A", "S")
        lqtdCargas.Text = cont
        contaPeso()
        controle = ""
        status(True)
        'dgCargas.DataSource = Nothing
        'associaGriCargas()
        'limpar()
    End Sub

    Private Sub bVaPara_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bVaPara.Click
        Try
            Dim frm As New frm_VaPara("combustivel")
            frm.ShowDialog()
            If (frm.codigo <> 0) Then
                'algum usuario foi clicado
                objA = New LotacaoBLL
                objA.localizar(frm.codigo)
                If (objA.pLot_codigo <> 0) Then
                    carregaCampos()
                    atual = objA.pLot_codigo
                End If
            End If
            status(True)
            frm = Nothing
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        Finally
            objA = Nothing
        End Try
    End Sub

    Private Sub bPesquisar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bPesquisar.Click
        gbConsulta.Enabled = True
        gbConsulta.Visible = True
        bPesquisar.Enabled = False
        Me.Location = New System.Drawing.Point(Me.Location.X, Me.Location.Y - 127)
        Me.Size = New Size(730, 730)
    End Sub

    Private Sub bPrimeiro_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bPrimeiro.Click
        carregaPrimeiro()
        atual = primeiro
        cont = 0
        contaCargas("A", "S")
        lqtdCargas.Text = cont
        contaPeso()
        status(True)
    End Sub

    Private Sub bAnterior_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bAnterior.Click
        Try
            objA = New LotacaoBLL
            Dim codigo = objA.localizarProxAnteriro("anterior", atual)
            If (codigo <> 0) Then
                objA.localizar(codigo)
                If (objA.pLot_codigo <> 0) Then
                    atual = objA.pLot_codigo
                    carregaCampos()
                    cont = 0
                    contaCargas("A", "S")
                    lqtdCargas.Text = cont
                    contaPeso()
                End If
            End If
            status(True)
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        Finally
            objA = Nothing
        End Try
    End Sub

    Private Sub bProximo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bProximo.Click
        Try
            objA = New LotacaoBLL
            Dim codigo = objA.localizarProxAnteriro("proximo", atual)
            If (codigo <> 0) Then
                objA.localizar(codigo)
                If (objA.pLot_codigo <> 0) Then
                    atual = objA.pLot_codigo
                    carregaCampos()
                    cont = 0
                    contaCargas("A", "S")
                    lqtdCargas.Text = cont
                    contaPeso()
                End If
            End If
            status(True)
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        Finally
            objA = Nothing
        End Try
    End Sub

    Private Sub bUltimo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bUltimo.Click
        carregaUltimo()
        atual = ultimo
        cont = 0
        contaCargas("A", "S")
        lqtdCargas.Text = cont
        contaPeso()
        status(True)
    End Sub

    Private Sub bAjuda_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bAjuda.Click
        Try
            Dim Ajuda As String
            Ajuda = Application.StartupPath
            Ajuda = Ajuda.Replace("\bin\Debug", "\Arquivo de Ajuda\index.html#MovimentacaoLotacaoCaminhao")
            Dim Ie As Object
            Ie = CreateObject("InternetExplorer.Application")
            Ie.Visible = True
            Ie.Navigate(Ajuda)
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub

    Private Sub bSair_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bSair.Click
        Me.Close()
    End Sub

    Private Sub frm_agendamento_carga_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Try

            carregaPrimeiroUltimo()
            carregaUltimo()
            bCancelar_Click(sender, e)
            'associaGrid()
            gbConsulta.Visible = False
            Me.Location = New System.Drawing.Point(Me.Location.X, Me.Location.Y + 127)
            Me.Size = New Size(723, 420)
            lqtdCargas.Text = cont
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub

    Private Sub bVeiculos_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bVeiculo.Click
        Try
            Dim frm As New frm_consultas
            objC = New VeiculosBLL
            frm.tipo = "veiculos_disponiveis"
            frm.ShowDialog()
            If (frm.codigo <> 0) Then
                objC.localizar(frm.codigo)
                tbCodVeiculo.Text = objC.pcodVeiculo
                tbCapacidadeVeiculo.Text = objC.pCapacidade

            End If
            frm = Nothing
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Critical, "Erro")
        Finally
            objC = Nothing
        End Try
    End Sub

    Private Sub dtCarregamento_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Try
            dtPrevPartida.Value = dtCarregamento.Value
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub

    Private Sub bCargas_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bCargas.Click
        Try
            Dim frm As New frm_consultas
            frm.tipo = "cargas_disponiveis"
            frm.ShowDialog()
            If (frm.codigo <> 0) Then
                tbCodCarga.Text = frm.codigo

            End If
            frm = Nothing
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Critical, "Erro")
        End Try
    End Sub

    Private Sub bAdicionar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bAdicionar.Click
        Try
            If (tbCodCarga.Text = "") Then
                MessageBox.Show("Escolha uma carga para adicionar", "TransToledo", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Else
                Dim existe As Integer = 0
                Dim objBLL As New LotacaoBLL()
                Dim linha As DataRow
                Dim tabela As New DataTable()


                tabela = objBLL.localizarDadosCarga(tbCodCarga.Text)
                If tabela.Rows.Count <> 0 Then
                    If (dgCargas.Rows.Count = 0) Then
                        linha = tab2.NewRow()
                        linha("car_codigo") = tbCodCarga.Text
                        linha("age_codigo") = tabela.Rows(0)("age_codigo")
                        linha("car_peso") = tabela.Rows(0)("car_peso")
                        linha("rot_distancia") = tabela.Rows(0)("rot_distancia")
                        linha("age_data_previsao") = tabela.Rows(0)("age_data_previsao")
                        tab2.Rows.Add(linha)
                        tab2.AcceptChanges()
                        tbCodCarga.Text = ""
                        Dim contador As Integer = contaCargas("A", "N")
                        lqtdCargas.Text = contador
                        contaPeso()
                    Else
                        Dim ret As Boolean = False
                        Dim cod As Integer = tbCodCarga.Text
                        For i As Integer = 0 To dgCargas.Rows.Count - 1 Step 1
                            If (dgCargas.Rows.Item(i).Cells("car_codigo").Value = cod) Then
                                MessageBox.Show("Essa carga ja está incluida", "TransToledo", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
                                tbCodCarga.Text = ""
                                ret = True
                            End If
                        Next
                        If (ret <> True) Then
                            linha = tab2.NewRow()
                            linha("car_codigo") = tbCodCarga.Text
                            linha("age_codigo") = tabela.Rows(0)("age_codigo")
                            linha("car_peso") = tabela.Rows(0)("car_peso")
                            linha("rot_distancia") = tabela.Rows(0)("rot_distancia")
                            linha("age_data_previsao") = tabela.Rows(0)("age_data_previsao")
                            tab2.Rows.Add(linha)
                            tab2.AcceptChanges()
                            tbCodCarga.Text = ""
                            protocolo = ""
                            valor = 0
                            Dim contador As Integer = contaCargas("A", "N")
                            lqtdCargas.Text = contador
                            contaPeso()
                        End If
                    End If
                Else
                    MessageBox.Show("Esta carga não está relaciona a nenhum agendamento.", "TransToledo", MessageBoxButtons.OK, MessageBoxIcon.Error)
                End If



            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub

    Private Sub bExcluirCarga_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bExcluirCarga.Click
        Dim i As Integer
        i = 0
        Try
            If dgCargas.SelectedRows.Count <> Nothing Then

                Dim objBLL As New LotacaoBLL()

                Dim tamanhogrid As Integer = dgCargas.Rows.Count - 1

                If controle = "I" Then

                    For i = 0 To tamanhogrid Step 1

                        If dgCargas.Rows.Item(i).Selected = True Then
                            dgCargas.Rows.RemoveAt(i)
                            tab2.Rows.RemoveAt(i)
                            Exit For
                        End If

                    Next
                ElseIf controle = "A" Then

                    For i = 0 To tamanhogrid Step 1

                        If dgCargas.Rows.Item(i).Selected = True Then
                            dgCargas.Rows.RemoveAt(i)
                            tab2.Rows.RemoveAt(i)
                            objBLL.excluirCarga(dgCargas.Rows.Item(i).Cells(0).Value, tbCodigo.Text)
                        End If

                    Next

                End If


                Dim contador As Integer = contaCargas("R", "N")
                lqtdCargas.Text = contador
                contaPeso()

                Else
            MessageBox.Show("Não existem itens para ser excluido!", "Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
                End If
            'Next
        Catch ex As Exception
            MessageBox.Show("Não foi possível excluir o item!", "Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub

    Private Sub bClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bClose.Click
        gbConsulta.Visible = False
        Me.Location = New System.Drawing.Point(Me.Location.X, Me.Location.Y + 127)
        Me.Size = New Size(723, 420)
        bPesquisar.Enabled = True
    End Sub

    Private Sub btnCarregar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCarregar.Click
        Try
            If tbCodVeiculo.Text <> "" Then

                carregaGriCargas()

                Dim contador As Integer = contaCargas("R", "N")
                lqtdCargas.Text = contador
                contaPeso()
            Else
                MessageBox.Show("Selecione um veículo para carregar!", "Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
            End If

        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub

    Private Sub dgCargas_SelectionChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
        If dgCargas.SelectedRows.Count <> 0 Then
            bExcluir.Enabled = True
        Else
            bExcluir.Enabled = False
        End If
    End Sub

    Private Sub btnBusca_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBusca.Click
        Dim status As String = ""
        Dim InicioCar As Date = Date.Parse("1111-11-11")
        Dim FimCar As Date = Date.Parse("1111-11-11")
        Dim InicioPrev As Date = Date.Parse("1111-11-11")
        Dim FimPrev As Date = Date.Parse("1111-11-11")
        Dim placa As String = ""

        Dim tab As DataTable
        Try
            If (ckCarregamento.Checked = True) Then
                InicioCar = dtInicioCar.Value
                FimCar = dtFimCar.Value
            End If
            If (ckPartida.Checked = True) Then
                InicioPrev = dtInicioPrev.Value
                FimPrev = dtFimPrev.Value
            End If
            If (ckVeiculo.Checked = True) Then
                placa = txtPlaca.Text
            End If
            If (ckAberto.Checked = True And ckFinal.Checked = False) Then
                status = "Aberto"
            ElseIf (ckFinal.Checked = True And ckAberto.Checked = False) Then
                status = "Finalizado"
            ElseIf (ckFinal.Checked = True And ckAberto.Checked = True) Then
                status = "AF"
            End If
            objA = New LotacaoBLL
            tab = objA.localizar(placa, InicioCar, FimCar, InicioPrev, FimPrev, status)
            dgConsulta.DataSource = tab
            dgConsulta.Columns("Expr1").HeaderText = "Código"
            dgConsulta.Columns("Expr1").Width = 90
            dgConsulta.Columns("lot_data").HeaderText = "Data Lotacao"
            dgConsulta.Columns("lot_data").Width = 120
            dgConsulta.Columns("lot_partida").HeaderText = "Previsão Partida"
            dgConsulta.Columns("lot_partida").Width = 120
            dgConsulta.Columns("vei_codigo").HeaderText = "Veículo"
            dgConsulta.Columns("vei_codigo").Width = 90
            dgConsulta.Columns("vei_placa").HeaderText = "Placa"
            dgConsulta.Columns("vei_placa").Width = 120
            dgConsulta.Columns("rot_codigo").HeaderText = "Rota"
            dgConsulta.Columns("rot_codigo").Width = 90
            dgConsulta.Columns("car_codigo").HeaderText = "Carga"
            dgConsulta.Columns("car_codigo").Width = 90
            dgConsulta.Columns("lot_status").HeaderText = "Status"
            dgConsulta.Columns("lot_status").Width = 100
            dgConsulta.Columns("cid_destino").HeaderText = "Cod. Destino"
            dgConsulta.Columns("cid_destino").Width = 90
            dgConsulta.Columns("nom_cidade").HeaderText = "Cidade"
            dgConsulta.Columns("nom_cidade").Width = 150
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
        objA = Nothing
    End Sub

    Private Sub bBuscaVei_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bBuscaVei.Click
        Try
            Dim frm As New frm_consultas
            objC = New VeiculosBLL
            frm.tipo = "Veiculos"
            frm.ShowDialog()
            If (frm.codigo <> 0) Then
                objC.localizar(frm.codigo)
                txtPlaca.Text = objC.pPlaca
            End If
            frm = Nothing
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Critical, "Erro")
        Finally
            objC = Nothing
        End Try
    End Sub

    Private Sub dgConsulta_CellDoubleClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgConsulta.CellDoubleClick
        Dim codigo As Integer
        Try
            If (e.RowIndex >= 0) Then
                codigo = dgConsulta.Rows(e.RowIndex).Cells("Expr1").Value
                objA = New LotacaoBLL
                objA.localizar(codigo)
                If (objA.pLot_codigo <> 0) Then
                    atual = objA.pLot_codigo
                    carregaCampos()
                    gbConsulta.Visible = False
                    Me.Location = New System.Drawing.Point(Me.Location.X, Me.Location.Y + 127)
                    Me.Size = New Size(723, 367)
                    bPesquisar.Enabled = True
                End If
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        Finally
            objA = Nothing
        End Try
    End Sub
End Class